package com.hangzhou.xyj.ui.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.BannerModel;
import com.hangzhou.xyj.ui.adapter.BannerAdapter;
import com.hangzhou.xyj.ui.widget.autoscrollviewpager.AutoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by neo.duan on 2015/12/22.
 * ????banner??
 */
public class FamousExpertBannerView extends CustomRelativeLayout {

    private Context mContext;
    private AutoScrollViewPager mViewPager;
    private LinearLayout mDotsContainer;
    //????λ????????
    private int previousPosition = 0;

    private List<BannerModel> mBannerList;
    private BannerAdapter mAdapter;

    public FamousExpertBannerView(Context context) {
        this(context, null);
    }

    public FamousExpertBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public FamousExpertBannerView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
        this.mContext = context;
        initView();
        initListener();
    }

    /**
     * ?????????
     */
    public void initView(){
        View.inflate(getContext(), R.layout.layout_famous_expert_banner, this);

        mViewPager = (AutoScrollViewPager) findViewById(R.id.vp_famous_expert_banner);
        /*setInterval(long) ????????????????????λ?????
        setDirection(int) ???????????????????????
        setCycle(boolean) ??????????????????true
        setScrollDurationFactor(double) ????ViewPager???????????????????????????????????????Ч??
        setStopScrollWhenTouch(boolean) ?????????ViewPager???????????????????true
        setSlideBorderMode(int) ???????????????????Item???????????????κβ??????????????????View??????
        setBorderAnimation(boolean) ?????????????????????????????????????????????????true*/

        mViewPager.setInterval(2000);
        mViewPager.setAutoScrollDurationFactor(4);
        mDotsContainer = (LinearLayout) findViewById(R.id.ll_famous_expert_dots);

        mViewPager.setAdapter(mAdapter = new BannerAdapter(mContext, null));
    }

    /**
     * ???????????
     */
    public void initListener() {
        //viewPager????????
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //???????л?????
                int newPosition = position % mBannerList.size();
                mDotsContainer.getChildAt(previousPosition).setSelected(false);
                mDotsContainer.getChildAt(newPosition).setSelected(true);
                previousPosition = newPosition;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    /**
     * TODO ????????
     */
    public void setData(List<BannerModel> bannerList) {
        if (bannerList == null) {
            bannerList = new ArrayList<>();
        }
        this.mBannerList = bannerList;
        mAdapter.update(bannerList);
        mViewPager.startAutoScroll();

        initDotsView(bannerList.size());
    }

    /**
     * ???????????
     */
    private void initDotsView(int size) {
        //???????
        mDotsContainer.removeAllViews();
        if (size <= 1) {//С??1???????
            return;
        }
        View view;
        android.widget.LinearLayout.LayoutParams lp;
        for (int i = 0; i < size; i++){
            //????????????????????
            view = new View(mContext);
            int params = (int) getResources().getDimension(R.dimen.banner_radius);
            lp = new android.widget.LinearLayout.LayoutParams(params,params);
            lp.leftMargin = (int) getResources().getDimension(R.dimen.banner_margin);
            view.setLayoutParams(lp);
            view.setBackgroundResource(R.drawable.selector_gray_red_dot);

            mDotsContainer.addView(view);
        }
        //???????????????
        previousPosition = 0;
        View pointView = mDotsContainer.getChildAt(previousPosition);
        if (pointView != null) {
            pointView.setSelected(true);
        }

    }
}
